package com.bonc.rms.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bonc.rms.service.PersonalService;

/**
 * 个人信息相关控制器
 */
@Controller("personalController")
public class PersonalController {
	@Autowired
	PersonalService personalService;

	/**
	 * 获取个人信息
	 */
	@RequestMapping(value = { "/personalResume", "/select/personalResume" })
	public ModelAndView personalResume(@RequestParam(value = "id", required = false) String id,
			HttpSession httpSession) {
		ModelAndView modelAndView = new ModelAndView("detailResume");

		List<Map<String, Object>> personalResumeList = personalService.personalResumePage(id);
		modelAndView.addObject("personalResumeList", personalResumeList);
		// 将得到的个人简历信息存入session以便在在关系图中使用
		Map<String, List<Map<String, Object>>> relationSchemaMap = new HashMap<>();
		relationSchemaMap.put("origin", personalResumeList);
		httpSession.setAttribute("relationSchemaMap", relationSchemaMap);
		return modelAndView;
	}

	/**
	 * 关系图获取新的个人信息    点击的 是 人！
	 */
	@RequestMapping(value = { "/newPerson" })
	@ResponseBody
	public Map personalResume(@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "nodePositionX", required = false) String nodePositionX,
			@RequestParam(value = "nodePositionY", required = false) String nodePositionY) {
		Map<String, Object> newPersonMap = personalService.personalResumePage(id).get(0);
		String companyStr = (String) newPersonMap.get("company");
		String educationStr = (String) newPersonMap.get("education");

		String[] companyArray = companyStr.split(" ");
		String[] educationArray = educationStr.split(" ");

		double X = Double.valueOf(nodePositionX);// 获得 的 点击位置X Y 坐标
		double Y = Double.valueOf(nodePositionY);
		double r = 50;
		double companyNodesX = X - r;
		double educationNodesX = X - r+10;
		
	  
		StringBuilder resultJson = new StringBuilder("[");
		StringBuilder zTreeJson = new StringBuilder("[");
		// 把公司 建成 点
		for (int i = 0; i < companyArray.length; i++) {
			double jianju =2*r / (companyArray.length+1);
			companyNodesX += jianju;
			double nodesY=0;
				if(i%2==0){
					nodesY =Y-Math.sqrt(r * r - (companyNodesX - X) * (companyNodesX - X));
				}else{
					
					nodesY =Y+Math.sqrt(r * r - (companyNodesX - X) * (companyNodesX - X));
				}
			
			
			resultJson.append("{data: {id: '").append(companyArray[i]).append("', name: '").append(companyArray[i])
					.append("', width: 3, height: 3, color: '#BFEFFF',sign:'company'}")
					.append(", position: {x: " + companyNodesX + ", y: " + nodesY + "} },");

			zTreeJson.append("{ id:'").append(companyArray[i])
			.append("',pId:'").append(id).append("',name:'")
			.append(companyArray[i]).append("'},");//, isParent:true
			
		}
		// 把学校 建成 点
		for (int i = 0; i < educationArray.length; i++) {
			double jianju =2*r / (educationArray.length*2);
			educationNodesX += jianju;
			r=40;
			double nodesY = 0;
			if(i%2==0){
				nodesY = Math.sqrt(r * r - (educationNodesX - X) * (educationNodesX - X)) + Y;
			}else{
				
				nodesY = Y-Math.sqrt(r * r - (educationNodesX - X) * (educationNodesX - X));
			}
			
			//double nodesY = Y-Math.sqrt(r * r - (companyNodesX - X) * (companyNodesX - X)) ;
			resultJson.append("{data: {id: '").append(educationArray[i]).append("', name: '").append(educationArray[i])
					.append("', width: 3, height: 3, color: '#99CCFF',sign:'education'}")
					.append(", position: {x: " + educationNodesX + ", y: " + nodesY + "} },");

			zTreeJson.append("{ id:'").append(educationArray[i])
			.append("',pId:'").append(id).append("',name:'")
			.append(educationArray[i]).append("'},");//, isParent:true
		}
		// 连线 公司
		for (int i = 0; i < companyArray.length; i++) {

			resultJson.append("{data: {id: '").append(i).append(companyArray[i]).append("', name: '")
					.append("', width: 0.1, color: '#426AB3', source: '").append(companyArray[i]).append("', target:'")
					.append(id).append("'} },");

		}
		// 连线 学校
		for (int i = 0; i < educationArray.length; i++) {

			resultJson.append("{data: {id: '").append(i).append(educationArray[i]).append("', name: '")
					.append("', width: 0.1, color: '#426AB3', source: '").append(educationArray[i])
					.append("', target:'").append(id).append("'} },");

		}

		resultJson.append("]");
		zTreeJson.append("]");
		//System.out.println(resultJson);
		Map jsonmap = new HashMap(); 
		jsonmap.put("resultJson", resultJson);
		jsonmap.put("zTreeJson", zTreeJson);
		 
		
		return jsonmap;
	}

	
	
	
	
	
	/**
	 * 关系图中获取关系   点击的是 公司 和学校
	 */
	@RequestMapping(value = "/ss")
	@ResponseBody
	public Map relationSchema(@RequestParam(value = "queryType", required = false) String queryType,
			@RequestParam(value = "queryValue", required = false) String queryValue,
			@RequestParam(value = "nodePositionX", required = false) String nodePositionX,
			@RequestParam(value = "nodePositionY", required = false) String nodePositionY) {

		double X = Double.valueOf(nodePositionX);// 获得 的 点击位置X Y 坐标
		double Y = Double.valueOf(nodePositionY);
		double r = 50;
		double nodesX = X - r;
		List<Map<String, Object>> relationSchemaList = personalService.relationSchema(queryType, queryValue);
		StringBuilder resultJson = new StringBuilder("[");
		StringBuilder zTreeJson = new StringBuilder("[");

		if (queryType.equals("company")) {

			for (int i = 0; i < relationSchemaList.size(); i++) {
				// (x-a)*(x-a)+(y-b)*(y-b)=r*r
				// x 的 范围： X-r < x < X+r
				if (relationSchemaList.get(i).get("id") != null) {
					double jianju = 2 * r / (relationSchemaList.size());
					nodesX += jianju;
					double nodesY = 0;
					if(i%2==0){
						nodesY = Math.sqrt(r * r - (nodesX - X) * (nodesX - X)) + Y;
					}else{
						nodesY = Y-Math.sqrt(r * r - (nodesX - X) * (nodesX - X)) ;
					}

					resultJson.append("{data: {id: '").append(relationSchemaList.get(i).get("id")).append("', name: '")
							.append(relationSchemaList.get(i).get("name"))
							.append("', width: 3, height: 3, color: '#e8aa8f',sign:'people'}")
							.append(", position: {x: " + nodesX + ", y: " + nodesY + "} },");
					
					zTreeJson.append("{ id:'").append(relationSchemaList.get(i).get("id"))
							.append("',pId:'").append("queryValue").append("',name:'")
							.append(relationSchemaList.get(i).get("name")).append("'},");//,isParent:true
					
					
				}
			}
			for (int i = 0; i < relationSchemaList.size(); i++) {
				if (relationSchemaList.get(i).get("id") != null) {
					resultJson.append("{data: {id: '").append(i).append(relationSchemaList.get(i).get("id"))
							.append("', name: '").append("', width: 0.1, color: '#426AB3', source: '")
							.append(queryValue).append("', target:'").append(relationSchemaList.get(i).get("id"))
							.append("'} },");
				}
			}

		} else if (queryType.equals("education")) {

			for (int i = 0; i < relationSchemaList.size(); i++) {
				// (x-a)*(x-a)+(y-b)*(y-b)=r*r
				// x 的 范围： X-r < x < X+r
				if (relationSchemaList.get(i).get("id") != null) {
					double jianju = 2 * r / (relationSchemaList.size());
					nodesX += jianju;
					double nodesY = 0;

					if(i%2==0){
						nodesY = Math.sqrt(r * r - (nodesX - X) * (nodesX - X)) + Y;
					}else{
						nodesY = Y-Math.sqrt(r * r - (nodesX - X) * (nodesX - X));
					}
					
					resultJson.append("{data: {id: '").append(relationSchemaList.get(i).get("id")).append("', name: '")
							.append(relationSchemaList.get(i).get("name"))
							.append("', width: 3, height: 3, color: '#e8aa8f',sign:'people'}")
							.append(", position: {x: " + nodesX + ", y: " + nodesY + "} },");
					
					zTreeJson.append("{ id:'").append(relationSchemaList.get(i).get("id"))
					.append("',pId:'").append("queryValue").append("',name:'")
					.append(relationSchemaList.get(i).get("name")).append("'},");//, isParent:true
				}
			}
			for (int i = 0; i < relationSchemaList.size(); i++) {
				if (relationSchemaList.get(i).get("id") != null) {
					resultJson.append("{data: {id: '").append(i).append(relationSchemaList.get(i).get("id"))
							.append("', name: '").append("', width: 0.1, color: '#426AB3', source: '")
							.append(queryValue).append("', target:'").append(relationSchemaList.get(i).get("id"))
							.append("'} },");
				}
			}

		}

		resultJson.append("]");
		zTreeJson.append("]");
		// System.out.println(resultJson);
		//System.out.println(zTreeJson);
		
		Map jsonmap = new HashMap(); 
		jsonmap.put("resultJson", resultJson);
		jsonmap.put("zTreeJson", zTreeJson);
		//return resultJson.toString();
		//return zTreeJson.toString();
		return jsonmap;
	}

	/**
	 * 下载
	 */
	@RequestMapping(value = "/download")
	public ResponseEntity<byte[]> downPath(@RequestParam(name = "path", required = true) String path)
			throws IOException {
		File file = new File(path);
		String[] str = path.split("\\\\");
		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("attachment",
				new String(str[str.length - 1].getBytes("gb2312"), "iso8859-1"));
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
	}
}
